using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace DAL
{
    public class tbBinhLuanDAL
    {
        private SqlConnection cn;
        public tbBinhLuanDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public tbBinhLuanDAL(tbBinhLuanDAL binhluan)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbBinhLuan binhluan)
        {
            SqlCommand cm = new SqlCommand("tbBinhLuan_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@khachhang", binhluan.khachhang);
            cm.Parameters.AddWithValue("@sanpham", binhluan.sanpham);
            cm.Parameters.AddWithValue("@noidung", binhluan.noidung);
            cm.Parameters.AddWithValue("@ngaygui", binhluan.ngaygui);
            cm.ExecuteNonQuery();
        }
        public void Update(tbBinhLuan binhluan)
        {
            SqlCommand cm = new SqlCommand("tbBinhLuan_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@khachhang", binhluan.khachhang);
            cm.Parameters.AddWithValue("@sanpham", binhluan.sanpham);
            cm.Parameters.AddWithValue("@noidung", binhluan.noidung);
            cm.Parameters.AddWithValue("@ngaygui", binhluan.ngaygui);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tbBinhLuan_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mabl", iD);
            cm.ExecuteNonQuery();
        }
        public tbBinhLuanCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbBinhLuan_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbBinhLuanCollection blCollection = new tbBinhLuanCollection();

            while (dr.Read())
            {
                tbBinhLuan binhluan = new tbBinhLuan();
                binhluan.mabl = int.Parse(dr.GetValue(0).ToString());
                binhluan.khachhang = int.Parse(dr.GetValue(1).ToString());
                binhluan.sanpham = int.Parse(dr.GetValue(2).ToString());
                binhluan.noidung = dr.GetValue(3).ToString();
                binhluan.ngaygui = dr.GetValue(4).ToString();
                blCollection.Add(binhluan);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return blCollection;
        }
    }
}
